package src.Packets;

import src.Util.Stream;
import src.Players.Player;

public class Walk
{

    public Walk(Player p, int pSize)
    {
        if(p == null || p.inStream == null)
        {
            return;
        }
        if(p.packetIndex == 248)
        {
            pSize -= 14;
        }
            p.newWalkCmdSteps = pSize - 5;
            p.newWalkCmdSteps /= 2;
            if(++p.newWalkCmdSteps > p.walkingQueueSize)
            {
                p.newWalkCmdSteps = 0;
            }
            int firstStepX = p.inStream.readSignedWordBigEndianA();
            firstStepX -= p.mapRegionX * 8;
            for(int i = 1; i < p.newWalkCmdSteps; i++)
            {
                p.newWalkCmdX[i] = p.inStream.readSignedByte();
                p.newWalkCmdY[i] = p.inStream.readSignedByte();
            }
            p.newWalkCmdX[0] = p.newWalkCmdY[0] = 0;
            int firstStepY = p.inStream.readSignedWordBigEndian();
            firstStepY -= p.mapRegionY * 8;
            boolean newWalkCmdIsRunning = p.inStream.readSignedByteC() == 1;
            for(int i = 0; i < p.newWalkCmdSteps; i++)
            {
                p.newWalkCmdX[i] += firstStepX;
                p.newWalkCmdY[i] += firstStepY;
            }
        if(p.packetIndex == 248)
        {
            mapWalkReset(p);
        }
        else if(p.packetIndex == 164)
        {
            mainWalkReset(p);
        }
        else if(p.packetIndex == 98)
        {
            clickReset(p);
        }
    }

    private void mainWalkReset(Player p)
    {
        if(p == null)
        {
            return;
        }
        if(p.faceToRequest != 65535)
        {
            p.requestFaceTo(65535);
        }
        p.itemPickup = false;
        p.npcOption1 = false;
        p.npcOption2 = false;
        p.npcOption3 = false;
        p.objectClick1 = false;
        p.objectClick2 = false;
        p.objectClick3 = false;
        p.attackingPlayer = false;
        p.attackingNPC = false;
        p.attackNPC = 0;
        p.attackPlayer = 0;
    }

    private void clickReset(Player p)
    {
        if(p == null)
        {
            return;
        }
        if(p.faceToRequest != 65535)
        {
            p.requestFaceTo(65535);
        }
        p.itemPickup = false;
        p.npcOption1 = false;
        p.npcOption2 = false;
        p.npcOption3 = false;
        p.objectClick1 = false;
        p.objectClick2 = false;
        p.objectClick3 = false;
        p.attackingPlayer = false;
        p.attackingNPC = false;
        p.attackNPC = 0;
        p.attackPlayer = 0;
    }

    private void mapWalkReset(Player p)
    {
        if(p == null)
        {
            return;
        }
        if(p.faceToRequest != 65535)
        {
            p.requestFaceTo(65535);
        }
        p.frame.removeAllWindows(p);
        p.itemPickup = false;
        p.npcOption1 = false;
        p.npcOption2 = false;
        p.npcOption3 = false;
        p.objectClick1 = false;
        p.objectClick2 = false;
        p.objectClick3 = false;
        p.attackingPlayer = false;
        p.attackingNPC = false;
        p.attackNPC = 0;
        p.attackPlayer = 0;
    }
}